#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <sys/time.h>

#define SPORT  10514
#define MAXLOG 1
#define MAX_PACKET 120

char syslog[] = 
{
0x73,0x00,0x00,0x3c,0x31,0x34,0x3e,0x4d,0x61,0x79,0x20,0x31,0x37,0x20,0x31,0x31,0x3a,0x32,0x31,0x3a,0x30,0x35,0x20,0x6b,0x65,0x76,0x65,0x6e,0x7a,0x68,0x75,0x2d,0x70,0x63,0x20,0x4b,0x73,0x65,0x6e,0x64,0x20,0x4b,0x4c,0x4f,0x47,0x3a,0x20,0x55,0x6e,0x52,0x65,0x47,0x69,0x53,0x74,0x45,0x72,0x45,0x64,0x20,0x56,0x65,0x52,0x73,0x49,0x6f,0x4e,0x20,0x54,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x74,0x65,0x73,0x74,0x20,0x6d,0x65,0x73,0x73,0x61,0x67,0x65,0x20,0x66,0x72,0x6f,0x6d,0x20,0x4b,0x69,0x77,0x69,0x20,0x53,0x79,0x73,0x6c,0x6f,0x67,0x20,0x53,0x65,0x6e,0x64,0x65,0x72,0x0a
};
int main(int argc, char *argv[])
{
    int sockfd;
    long long counter,ipos,idebug;
    char buff[MAX_PACKET];
    //struct hostent *host;
    struct sockaddr_in serv_addr;

    /*host = gethostent();
    if(NULL == host)
    {
        printf("error to get the hostent %d \n",errno);
        exit(1);
    }*/

    //sockfd = socket(AF_INET,SOCK_DGRAM,0);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        printf("error to create the socket, error %d\n",errno);
        exit(1);
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port   = htons(SPORT);
    if(argc!=3)
        inet_aton("10.1.136.8",&serv_addr.sin_addr.s_addr);
    else
    {
        printf("server is %s\n",argv[1]);
        inet_aton(argv[1],&serv_addr.sin_addr.s_addr);
    }
    //iserv_addr.sin_addr   = *((struct in_addr*)host->h_addr);
    bzero(&(serv_addr.sin_zero),8);
    //idebug = 4*1024*1024;
    //setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&idebug,4);
    //setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&idebug,4);

    if(connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(struct sockaddr))== -1)
    {
        printf("error to call the connect %d\n",errno);
        exit(1);
    }
    
    /*construct the packet*/
    if(argc != 3)
        counter = 1;
    else
        counter = atoll(argv[2]);
    printf("counter is %d\n",counter);
    
    for(idebug =0;idebug<sizeof(syslog);idebug++)
    {
        printf("%02x",syslog[idebug]);
    }
    printf("\n\n");
    srand(time(NULL));
    for(ipos = 0; ipos < counter; ipos++)
    {
        syslog[2]= rand()&1;
        if(-1 == send(sockfd,syslog,sizeof(syslog),0))
        {
            perror("error to send");
            printf("counter %d error to send the traffic %d \n",ipos,errno);
            exit(1);
        }
       // usleep(1);
    }
    return 1;
}

